var optionType="";
var Image0="";
var familyId = "";
var openId = "";
var familyName = "";
$(function(e){
	optionType = $("#tousu").val();
	receiveData();
	dataInit();
	updateImg();
});

function dataInit(){
	codes = $.req("code");
	
	AjaxPostUtil.request({url : path+"1853f065ae4845f2a235af6449c18767", params:{codes:codes}, type : 'json', callback : function(json){
		console.log(json);
		if(json.returnCode == "-999"){//重新登记
			openId = json.bean.openId;
			
			location.href='../../html/wechat_signup/wechat_signup.html?openId='+openId;
		}else if(json.returnCode == "-888"){
			 //关闭当前页面，返回到微信主页面
			 swal({ title : "提示", text : "信息正在审核中...", type : "warning" ,closeOnConfirm:false, closeOnCancel:false },function(){
					wx.closeWindow();
			});
		}else if(json.returnCode == "-777"){
			swal({ title : "提示", text : "信息审核失败，请重新登记！", type : "warning", closeOnConfirm:false, closeOnCancel:false}, function(){
				openId = json.bean.openId;
				location.href='../../html/wechat_signup/wechat_signup.html?openId='+openId;
			});
			
		}else if(json.returnCode == 0){
			openId = json.bean.openId;
			familyId = json.bean.id;
			familyName = json.bean.familyName;
		}else{
			swal({ title : "警告", text : "请重新打开页面", type : "warning" ,closeOnConfirm:false, closeOnCancel:false },function(){
				wx.closeWindow();
			});
		}
	}});
	eventInit();
}

function eventInit(){
	
	$('body').on('click', '#tousu', function(e){
		optionType = $("#tousu").val();
		$("#tousu").addClass("on");
		$("#jianyi").removeClass("on");
		$("#biaoyang").removeClass("on");
	});
	$('body').on('click', '#jianyi', function(e){
		optionType = $("#jianyi").val();
		$("#jianyi").addClass("on");
		$("#tousu").removeClass("on");
		$("#biaoyang").removeClass("on");
		
	});
	$('body').on('click', '#biaoyang', function(e){
		optionType = $("#biaoyang").val();
		$("#tousu").removeClass("on");
		$("#jianyi").removeClass("on");
		$("#biaoyang").addClass("on");
	});
	
	$('body').on('click', '#optionBtn', function(e){
		if(isNull($("#optionText").val())){
			swal({ title : "警告", text : "请填写反馈信息!", type : "warning" }); 
			return;
		}else{
			var params = {
					optionText : $("#optionText").val(),
					optionType : optionType,
					userId : familyId,
					familyName :familyName,
					openId : openId,
					optionImg : Image0
			}
			
			AjaxPostUtil.request({url : path+"55259b07a4ae4d1397839b9382fd9610", params : params, type : 'json', callback : function(json){
        		if(json.returnCode == 0){
        			console.log(json);
        			swal({ title : "成功", text : "提交成功！", type : "success" ,closeOnConfirm:false, closeOnCancel:false }, function(){
    					wx.closeWindow();
    				}); 
        			
        		}else{
        			swal({ title : "错误", text : '提交失败！', type : "error" }); 
        		}
        	}});
		}
	});
}
function updateImg() {
	
	$("#ImageZero").uploadPreview({
		Img : "ImgZero"
	});
	$("#ImageZero").fileupload(
	{
		url : path + "dd25d8a6660749acb7a7ab8880843815",
		disableImageResize : /Android(?!.*Chrome)|Opera/ .test(window.navigator.userAgent),
		autoUpload : true,// 是否自动上传
		maxFileSize : 999000,
		imageMaxWidth : 1000,
		imageMaxHeight : 800,
		imageQuality : 0.6,
		dataType : 'json',
		acceptFileTypes : /(\.|\/)(jpe?g|png|svg)$/i
	}).on('fileuploadadd', function(e, data) {
		var uploadFile = data.files[0];
		fileName = uploadFile.name;
		var reg = ".(jpg|png|jpeg|svg)$";
		var re = new RegExp(reg);
		
		if (fileName.length > 50) {
			swal({ title : "错误", text : "文件名长度不能大于50", type : "error" }); 
			$.staticPic('ImgZero', 'logoDiv');
			return false;
		} else if (!re.test(fileName.toLowerCase())) {
			swal({ title : "错误", text : "文件必须为jpg/jpeg/png格式", type : "error" });
			$.staticPic('ImgZero', 'logoDiv');
			return false;
		} else if (uploadFile.size > 6000000) { // 2mb
			swal({ title : "错误", text : "文件不能大于2M", type : "error" });
			$.staticPic('ImgZero', 'logoDiv');
			return false;
		}
	}).on('fileuploaddone', function(e, data) {
		if (isNull(data.result.bean)) {
			$.staticPic('ImgZero', 'logoDiv');
			swal({ title : "错误", text : data.result.returnMessage, type : "error" });
		} else {
			 Image0  = data.result.bean.optionPath;
			 swal({ title : "成功", text : "上传成功", type : "success" });
		}
	}).on('fileuploadfail', function(e, data) {
		$.staticPic('ImgZero', 'logoDiv');
		swal({ title : "错误", text : "上传失败，图片格式不正确", type : "error" });
	});
}

